﻿@using Fap.AspNetCore.Controls.JqGrid
@model Fap.AspNetCore.ViewModel.JqGridViewModel
@{
    Layout = null;
    //增加操作列
    Column column = new Column("oper");
    column.SetLabel("操作");
    column.SetSortable(false);
    column.SetWidth(70);
}
<title><fap-multilang lang-key="job_schedule" default-content="任务调度"></fap-multilang></title>
<div class="row">
    <div class="col-xs-12 col-sm-3">
        <div class="widget-box">
            <div class="widget-header widget-header-flat">
                <h4 class="widget-title"><fap-multilang lang-key="job_group" default-content="任务组"></fap-multilang></h4>
                <fap-tree-button id="treejobgroup" tree-id="tree-jobgroup" description="任务组"></fap-tree-button>

            </div>

            <div class="widget-body">
                <div class="widget-main">
                    <fap-tree id="jobgroup" is-async="true" get-url="/System/Api/Manage/JobGroup/" edit-url="/System/Api/Manage/OperJobGroup"></fap-tree>
                </div>
            </div>
        </div>
    </div>
    <div class="col-xs-12 col-sm-9">
        <div class="widget-box">
            <div class="widget-header widget-header-flat">
                <h4 class="widget-title"><fap-multilang lang-key="job_schedule" default-content="任务调度"></fap-multilang></h4>

                <div class="widget-toolbar">
                    <a href="#" data-action="fullscreen" class="orange2">
                        <i class="ace-icon fa fa-expand"></i>
                    </a>
                </div>
                <div class="widget-toolbar">
                    <fap-button btn-tag="link" id="btnJobLog" content="调度日志" icon-before="fa fa-folder-open-o"></fap-button>
              
                </div>
            </div>
            <div class="widget-body">
                <div class="widget-main">
                    <fap-grid id="fapjob" attach-column="column" grid-model="Model" auto-width="true"  wrapper="widget-main" on-form-init-add="afterInitFormCallback" shrink-fit="false" on-grid-complete="initMenu"></fap-grid>

                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function initMenu() {
         var ids = jQuery("#grid-fapjob").jqGrid('getDataIDs');
        for (var i = 0; i < ids.length; i++) {
            var de = '';
            var cl = ids[i];
            var ret = jQuery('#grid-fapjob').jqGrid('getRowData', cl);
            var fid = ret.Fid;
            var execState = ret.ExecState;//执行状态
            var jobState = ret.JobState;//任务状态
            if (jobState === 'Enabled') {
                if (execState === 'Execing') {
                    //执行中
                    de += "<a  href='javascript:void(0)' onclick='operJob(\"" + fid + "\",\"Suspend\")'>暂停</a>  ";

                } else if (execState === 'NoExec') {
                    //未执行
                    de += "<a  href='javascript:void(0)' onclick='operJob(\"" + fid + "\",\"Add\")'>加入</a>  ";

                } else if (execState === 'Suspend') {
                    //暂停
                    de += "<a  href='javascript:void(0)' onclick='operJob(\"" + fid + "\",\"Resume\")'>恢复</a>";
                }
                de += "   <a  href='javascript:void(0)' onclick='operJob(\"" + fid + "\",\"Disabled\")'>禁用</a>";
            } else {
                 de += "<a  href='javascript:void(0)' onclick='operJob(\"" + fid + "\",\"Enabled\")'>启用</a>  ";
            }
            jQuery('#grid-fapjob').jqGrid('setRowData', ids[i], { oper: de });
        }
    }
    function operJob(fid, param) {
        $.post("@Url.Content("~/System/Api/Manage/OperJobState")", { fid: fid, state: param }, function (data) {
            if (data) {
                $.msg("操作成功");
                $("#grid-fapjob").jqGrid('setGridParam', {
                    datatype: 'json',
                    page: 1
                }).trigger("reloadGrid"); //重新载入
            }
        })
    }
    var afterInitFormCallback = function () {
        var tree = $('#tree-jobgroup').jstree(true);
        var sel = tree.get_selected();
        if (sel.length) {
            $("#JobGroup").val(sel[0]).next().val(tree.get_node(sel[0]).text);
        }
        $("#ExecState").val('NoExec');
    }
    var scripts = [null, null]
    $('.page-content-area').ace_ajax('loadScripts', scripts, function () {
        jQuery(function ($) {
            $(document).one('ajaxloadstart.page', function (e) {
                $('#tree-jobgroup').jstree('destroy');
            })
            $('#tree-jobgroup').on("changed.jstree", function (e, data) {
                if (data && data.selected && data.selected.length) {
                    var jobgroupId = data.selected[0];
                    //显示所有角色组的角色
                    if (jobgroupId == "0") {
                        $("#grid-fapjob").jqGrid('setGridParam', {
                            datatype: 'json',
                            postData: { filters: {} }, //发送数据
                            page: 1
                        }).trigger("reloadGrid"); //重新载入
                        return;
                    }
                    var jobgroupName = data.node.text;

                    var filter = '{"groupOp":"AND","rules":[{"field":"JobGroup","op":"eq","data":"' + jobgroupId + '"}]}'
                    $("#grid-fapjob").jqGrid('setGridParam', {
                        datatype: 'json',
                        postData: { 'filters': filter }, //发送数据
                        page: 1
                    }).trigger("reloadGrid"); //重新载入
                }
                //console.log(data.selected);
            });
          
            $("#btnJobLog").on(ace.click_event, function () {
                var rowData = getSelectedRow("grid-fapjob");
                if (rowData == null) {
                    return;
                }
                var url = "@Url.Content("~/System/Manage/JobLog")/" + rowData.JobCode+"?menuUrl=~/System/Manage/JobMgt";

                var dialog = bootbox.dialog({
                    title: $.lang('job_log',"调度日志"),
                    message: '<p><i class="fa fa-spin fa-spinner"></i> Loading...</p>',
                    size: "large",
                    footer: false,
                   // buttons: buttons
                });
                dialog.init(function () {
                    $.get(url, function (ev) {
                        dialog.find('.bootbox-body').html(ev);

                    });
                });
            });
            
        })
    })
</script>
